package com.umfwechat.h5.controller.fundQuery;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import net.sf.json.JSONArray;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.umfwechat.common.code.RetCode;
import com.umfwechat.common.constant.CacheConsts;
import com.umfwechat.common.constant.CommonConsts;
import com.umfwechat.common.constant.ConfigConsts;
import com.umfwechat.common.constant.MsgConsts;
import com.umfwechat.common.constant.PropertyConsts;
import com.umfwechat.common.constant.ValueConsts;
import com.umfwechat.common.prop.UmfPropModel;
import com.umfwechat.util.DateTimesUtil;
import com.umfwechat.util.FormatUtil;
import com.umfwechat.util.HttpUtil;
import com.umfwechat.util.RedisUtil;
import com.umfwechat.util.ReqMessageUtil;
import com.umfwechat.util.ResultJsonUtil;
import com.umfwechat.util.SortUtil;
import com.umfwechat.util.StringUtil;
import com.umpay.sp.util.AmountUtil;

@Controller
@RequestMapping("/h5/fundQuery")
public class FundQueryController {
	
	private static Logger logger = LoggerFactory.getLogger(FundQueryController.class);
	
	//获取基金渠道编码
	private	String fundChannelCode = UmfPropModel.INSTANCE.getPropValue(ConfigConsts.FUNDMERID);
	//获取基金编号
	private	String fundNo = UmfPropModel.INSTANCE.getPropValue(ConfigConsts.FUNDNO);
	//fpbg-ip端口
	private String serviceuUrl_fpbg = UmfPropModel.INSTANCE.getPropValue(ConfigConsts.SERVICEURL_FPBG);
	
	/**
	 * 查询七日年化率、银行活期利率
	 * @param request
	 * @param merId
	 * @return
	 */
	@RequestMapping(value = "/annualRate",produces="application/json;charset=UTF-8")
	@ResponseBody
	public String annualRate(){		
		logger.info("【未开户用户主页初始化开始】"); 
		Map<String, Object> result = new HashMap<String, Object>();
		
		//从配置文件中获取银行活期利率
		String bankRateCall = UmfPropModel.INSTANCE.getPropValue(ConfigConsts.BANKRATECALL);		
		
		if (StringUtil.isEmpty(bankRateCall)) {
			logger.info("【获取银行活期利率失败】");
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433044);
		}
		
		if (StringUtil.isEmpty(fundChannelCode)) {
			logger.info("【获取基金渠道编码失败】");
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433044);
		}
		if (StringUtil.isEmpty(fundNo)) {
			logger.info("【获取基金编码失败】");
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433044);
		}
		
		String annualRate = CommonConsts.SPACE;
		String dateInterval=CacheConsts.ONEMONTH;//一月
		String dateSlot=ValueConsts.STATE_TWO;
		Map<String, Object> queryList = this.commonQueryList(dateSlot, dateInterval);
		if(RetCode.FAIL.equals(queryList.get(StringUtil.trim(CommonConsts.RETCODE)))){
			logger.info("【万分收益及年化率列表数失败】");
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433044);
		}
		List<Map<String,String>> list =(List<Map<String,String>>) queryList.get(CommonConsts.FUNDRATELIST);
		if(StringUtil.isNotEmpty(StringUtil.trim(list))){
			int size = list.size();
			logger.info("【万分收益及年化率列表数】size="+size);
			if(size!=0){
				Map<String, String> map = list.get(size-1);
				annualRate = map.get(CommonConsts.ANNUAL7D);
				logger.info("【获取最近一条数据annualRate="+annualRate);
			}
		}
		
		//银行活期利率
		result.put(CommonConsts.BANKRATECALL,bankRateCall);
		//年化率
		result.put(CommonConsts.SYL,StringUtil.trim(FormatUtil.setScale4(annualRate)));
		logger.info("【未开户用户主页初始化成功，将数据返回页面】result="+result);
		return ResultJsonUtil.resultJson(RetCode.SUCCESS,CommonConsts.SPACE,result);
	}
	


	/**
	 * 基金产品公共查询
	 * @return
	 */
	@RequestMapping(value = "/commonQuery",produces="application/json;charset=UTF-8")
	@ResponseBody
	public String commonQuery(){		
		logger.info("【基金产品公共查询初始化开始】");
		Map<String, Object> result = new HashMap<String, Object>();
		
		//从配置文件中获取基金名称
		String fullName = UmfPropModel.INSTANCE.getPropValue(ConfigConsts.FUNDNAME);		
		
		if (StringUtil.isEmpty(fullName)) {
			logger.info("【从配置文件中获取基金名称失败】");
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433044);
		}
		if (StringUtil.isEmpty(fundNo)) {
			logger.info("【获取基金编码失败】");
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433044);
		}
		if (StringUtil.isEmpty(fundChannelCode)) {
			logger.info("【获取基金渠道编码失败】");
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433044);
		}
		if (StringUtil.isEmpty(serviceuUrl_fpbg)) {
			logger.info("【获取七日年化率路径ip失败】");
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433044);
		}
		//获取当前日期YYYYMMDD
		String string = DateTimesUtil.date8();
		//从redis中获取基金最低认购
		String minSubscribAmount = RedisUtil.get(CacheConsts.SAASWX+CacheConsts.MINSUBSCRIBAMOUNT+string);
		
		if (StringUtil.isEmpty(minSubscribAmount)) {
			logger.info("【从redis中获取基金最低认购失败】");
			if (StringUtil.isEmpty(serviceuUrl_fpbg)) {
				logger.info("【获取路径端口失败】");
				return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433044);
			}
			String minSubscribAmountUrl = UmfPropModel.INSTANCE.getPropValue(ConfigConsts.FPBG_S01002);
			if (StringUtil.isEmpty(minSubscribAmountUrl)) {
				logger.info("【获取基金最低认购路径失败】");
				return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433044);
			}
			
			Map<String, Object> params = new HashMap<String, Object>();
			// 输入参数
			com.umfwechat.common.ReqMessage makeFundReq = ReqMessageUtil.makeFundReq();
			params.put(CommonConsts.RPID,makeFundReq.getRpid());
			params.put(CommonConsts.REQDATE, makeFundReq.getReqDate());//日期
			params.put(CommonConsts.REQTIME, makeFundReq.getReqTime());//时间
			params.put(CommonConsts.FUNDNO,fundNo);
			//params.put(CommonConsts.VERSION,ValueConsts.VERSION);
			//params.put(CommonConsts.FUNDCHANNELCODE,fundChannelCode);
			Map<String, Object> res=null;
			try {
				logger.info("【基金最低认购查询】httpPostForm2Xml请求数据：params"+params+ "访问路径url="+ serviceuUrl_fpbg+minSubscribAmountUrl);	
				res = HttpUtil.httpPostForm2Xml(serviceuUrl_fpbg+minSubscribAmountUrl, params);
				logger.info("【基金最低认购查询】httpPostForm2Xml返回结果："+res);
				if(!RetCode.SUCCESS.equals(StringUtil.trim(res.get(CommonConsts.RETCODE)))) { // 查询失败
					minSubscribAmount=RedisUtil.get(CacheConsts.SAASWX+CacheConsts.MINSUBSCRIBAMOUNT+CacheConsts.YESTERDAY);
					logger.info("【掉基金最低认购接口失败!-从redis中获取昨天的数据】minSubscribAmount="+minSubscribAmount);
				}else{
					minSubscribAmount = StringUtil.trim(res.get(CommonConsts.MINSUBSCRIBAMOUNT));
					logger.info("【调用接口获取基金最低认购】minSubscribAmount="+minSubscribAmount);
					if (StringUtil.isEmpty(minSubscribAmount)) {
						logger.info("【调用接口获取基金最低认购为空】");
						minSubscribAmount=RedisUtil.get(CacheConsts.SAASWX+CacheConsts.MINSUBSCRIBAMOUNT+CacheConsts.YESTERDAY);
						logger.info("【掉基金最低认购接口失败!-从redis中获取昨天的数据】minSubscribAmount="+minSubscribAmount);
					}else{
						RedisUtil.set(CacheConsts.SAASWX+CacheConsts.MINSUBSCRIBAMOUNT+string,ValueConsts.ONE_DAY,minSubscribAmount);
						logger.info("【重新调用接口获取基金最低认购放入redis中】");
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				minSubscribAmount=RedisUtil.get(CacheConsts.SAASWX+CacheConsts.MINSUBSCRIBAMOUNT+CacheConsts.YESTERDAY);
				logger.info("【掉基金最低认购接口异常!-从redis中获取昨天的数据】minSubscribAmount="+minSubscribAmount+"异常"+e);
			}
			minSubscribAmount = StringUtil.trim(minSubscribAmount);
			if(StringUtil.isNotEmpty(minSubscribAmount)){
				RedisUtil.set(CacheConsts.SAASWX+CacheConsts.MINSUBSCRIBAMOUNT+CacheConsts.YESTERDAY,ValueConsts.ONE_WEEK,minSubscribAmount);
			}
			
		}
		
		//从获取七日年化率
		String annualRate = CommonConsts.SPACE;
		String dateInterval=CacheConsts.ONEMONTH;//一月
		String dateSlot=ValueConsts.STATE_TWO;
		Map<String, Object> queryList = this.commonQueryList(dateSlot, dateInterval);
		if(RetCode.FAIL.equals(queryList.get(StringUtil.trim(CommonConsts.RETCODE)))){
			logger.info("【万分收益及年化率列表数失败】");
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433044);
		}
		List<Map<String,String>> list =(List<Map<String,String>>) queryList.get(CommonConsts.FUNDRATELIST);
		if(StringUtil.isNotEmpty(StringUtil.trim(list))){
			int size = list.size();
			logger.info("【万分收益及年化率列表数】size="+size);
			if(size!=0){
				Map<String, String> map = list.get(size-1);
				annualRate = map.get(CommonConsts.ANNUAL7D);
				logger.info("【获取最近一条数据annualRate="+annualRate);
			}
		}
		
		//从redis中获取累计交易金额
		String cumulativeSum = RedisUtil.get(CacheConsts.SAASWX+CacheConsts.CUMULATIVESUM+string);
		String strokeCount = RedisUtil.get(CacheConsts.SAASWX+CacheConsts.STROKECOUNT+string);
		if(StringUtil.isEmpty(cumulativeSum)){
			//获取当前时间
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.DATE, -1);
		    String endDate = DateTimesUtil.date8(calendar.getTime());
			
			//基金信息总额查询路径
			String fpbg_S01004 = UmfPropModel.INSTANCE.getPropValue(ConfigConsts.FPBG_S01004);
			if (StringUtil.isEmpty(fpbg_S01004)) {
				logger.info("【获取查询基金信息接口失败】");
				return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433044);
			}
			//开始时间
			String startDate = UmfPropModel.INSTANCE.getPropValue(ConfigConsts.FUNDSUMAMOUNT_STARTDATE);
			if(StringUtil.isEmpty(startDate)){
				logger.info("【获取查询基金信息接口输入参开始时间失败】");
				return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433044);
			}
			Map<String, Object> params = new HashMap<String, Object>();
			// 输入参数
			com.umfwechat.common.ReqMessage makeFundReq = ReqMessageUtil.makeFundReq();
			params.put(CommonConsts.RPID,makeFundReq.getRpid());
			params.put(CommonConsts.REQDATE, makeFundReq.getReqDate());//日期
			params.put(CommonConsts.REQTIME, makeFundReq.getReqTime());//时间
			params.put(CommonConsts.FUNDNO,fundNo);//基金编号
			params.put(CommonConsts.VERSION,ValueConsts.VERSION);
			params.put(CommonConsts.ENDDATE,endDate);
			params.put(CommonConsts.STARTDATE,startDate);
			//params.put(CommonConsts.FUNDCHANNELCODE,fundChannelCode);//基金渠道编号
			Map<String, Object> res=null;
			try {
				logger.info("【获取交易总额查询】httpPostForm2Xml请求数据：params"+params+ "访问路径url="+ serviceuUrl_fpbg+fpbg_S01004);	
				res = HttpUtil.httpPostForm2Xml(serviceuUrl_fpbg+fpbg_S01004, params);
				logger.info("【获取交易总额查询】httpPostForm2Xml返回结果："+res);
				if(RetCode.SUCCESS.equals(StringUtil.trim(res.get(CommonConsts.RETCODE)))) { // 查询成功
					logger.info("【获取交易总额查询成功】");
					strokeCount = StringUtil.trim(res.get(CommonConsts.FUNDSUMNUMBER));
					cumulativeSum = StringUtil.trim(res.get(CommonConsts.FUNDSUMAMOUNT));
					if(StringUtil.isNotEmpty(strokeCount)){
						RedisUtil.set(CacheConsts.SAASWX+CacheConsts.STROKECOUNT+string,ValueConsts.ONE_DAY,strokeCount);
						logger.info("【将掉接口获取的基金总笔数重新放入redis中】="+strokeCount);
					}
					if(StringUtil.isNotEmpty(cumulativeSum)){
						RedisUtil.set(CacheConsts.SAASWX+CacheConsts.CUMULATIVESUM+string,ValueConsts.ONE_DAY,cumulativeSum);
						logger.info("【将掉接口获取的基金总交易额重新放入redis中】="+cumulativeSum);
					}
				}else{
					cumulativeSum=RedisUtil.get(CacheConsts.SAASWX+CacheConsts.CUMULATIVESUM+CacheConsts.YESTERDAY);
					strokeCount=RedisUtil.get(CacheConsts.SAASWX+CacheConsts.STROKECOUNT+CacheConsts.YESTERDAY);
					logger.info("【获取前一天数据】基金总交易额="+cumulativeSum+"基金总笔数="+strokeCount);
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error(String.format("【获取交易额查询异常】"+e));
				cumulativeSum=RedisUtil.get(CacheConsts.SAASWX+CacheConsts.CUMULATIVESUM+CacheConsts.YESTERDAY);
				strokeCount=RedisUtil.get(CacheConsts.SAASWX+CacheConsts.STROKECOUNT+CacheConsts.YESTERDAY);
				logger.info("【获取前一天数据】基金总交易额="+cumulativeSum+"基金总笔数="+strokeCount);
			}
			strokeCount=StringUtil.trim(strokeCount);
			cumulativeSum=StringUtil.trim(cumulativeSum);
			if(StringUtil.isNotEmpty(strokeCount)){
				RedisUtil.set(CacheConsts.SAASWX+CacheConsts.STROKECOUNT+CacheConsts.YESTERDAY,ValueConsts.ONE_WEEK,strokeCount);
			}
			if(StringUtil.isNotEmpty(cumulativeSum)){
				RedisUtil.set(CacheConsts.SAASWX+CacheConsts.CUMULATIVESUM+CacheConsts.YESTERDAY,ValueConsts.ONE_WEEK,cumulativeSum);
			}
			
		}
		//cumulativeSum="10000000000";TODO
		String fundSumAmount = UmfPropModel.INSTANCE.getPropValue(ConfigConsts.FUNDSUMAMOUNT);//配置文件基金总金额
		
		BigDecimal fSum = null;
		BigDecimal cSum = null;
		if(StringUtil.isEmpty(fundSumAmount)){
			fSum=new BigDecimal(0);
		}else{
			fSum=new BigDecimal(fundSumAmount);
		}
		if(StringUtil.isEmpty(cumulativeSum)){
			cSum = new BigDecimal(0);
		}else{
			cSum=new BigDecimal(cumulativeSum);
		}
		if(fSum.compareTo(cSum)==1){//比较大小
			cumulativeSum=fSum.toString();
			logger.info("实际交易总额比虚拟总额小cumulativeSum="+cumulativeSum);
		}
		cumulativeSum = AmountUtil.Cent2Dollar(cumulativeSum);
		
		//获取当前时间
		Calendar calendar = Calendar.getInstance();
        //小时
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        //分钟
        int minute = calendar.get(Calendar.MINUTE);
        //星期
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        logger.info("【买入时间"+string+"星期"+day+hour+":"+minute);
		if(day==2 || day==3 || day==4 || day==5 || day==6){//星期一、二、三、四、五
			if(hour>=15){
				calendar.add(Calendar.DATE, 2);
				if(day==5){
					calendar.add(Calendar.DATE, 2);
				}
			}else{
				calendar.add(Calendar.DATE, 1);
			}
			if(day==6){
				calendar.add(Calendar.DATE, 2);
			}
		}else if(day == 7){//星期六
			calendar.add(Calendar.DATE, 3);
		}else{//星期日
			calendar.add(Calendar.DATE, 2);
		}
		//开始计算收益时间
		String revenueTime = DateTimesUtil.date10(calendar.getTime());
		calendar.add(Calendar.DATE, 1);
		//收益到账时间
		String toAccountTime = DateTimesUtil.date10(calendar.getTime());
	    logger.info("【开始计算收益="+revenueTime+"收益到账="+toAccountTime);
	    
	    if(StringUtil.isEmpty(minSubscribAmount)){//最低认购为null
	    	minSubscribAmount=ValueConsts.ZERO;
		}
		result.put(CommonConsts.CUMULATIVESUM,cumulativeSum); //累计交易额  
		result.put(CommonConsts.STROKECOUNT,strokeCount);// 交易笔数
		result.put(CommonConsts.FCODE,fundNo);
		result.put(CommonConsts.FULLNAME,fullName);
		result.put(CommonConsts.SYL,StringUtil.trim(FormatUtil.setScale4(annualRate)));
		result.put(CommonConsts.MINSUBSCRIBAMOUNT,AmountUtil.Cent2Dollar2(minSubscribAmount));
		result.put(CommonConsts.REVENUETIME,revenueTime);
		result.put(CommonConsts.TOACCOUNTTIME,toAccountTime);
		logger.info("【基金产品公共查询初始化成功，将数据返页面】result="+result);
		return ResultJsonUtil.resultJson(RetCode.SUCCESS,CommonConsts.SPACE,result);
	}
	
	/**
	 * 年化率、万份收益图表库
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/chartQuery",produces="application/json;charset=UTF-8")
	@ResponseBody
	public String chartQuery(String dateSlot){		
		logger.info("【年化率、万份收益图表库开始】");
		Map<String, Object> result = new HashMap<String, Object>();
		
		if (StringUtil.isEmpty(dateSlot)) {
			logger.info("【从页面获取类型为空】");
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433044);
		}
		if (StringUtil.isEmpty(fundNo)) {
			logger.info("【获取基金编码失败】");
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433044);
		}
		if (StringUtil.isEmpty(fundChannelCode)) {
			logger.info("【获取基金渠道编码失败】");
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433044);
		}
		String dateInterval= null;
		if(ValueConsts.STATE_ONE.equals(dateSlot)){//七日年化收益率折线图
			dateInterval=CacheConsts.SEVENDAY;
		}else if(ValueConsts.STATE_TWO.equals(dateSlot)){//一月年化收益率折线图
			dateInterval=CacheConsts.ONEMONTH;
		}else if(ValueConsts.STATE_THREE.equals(dateSlot)){//三月年化收益率折线图
			dateInterval=CacheConsts.THREEMONTH;
		}else if(ValueConsts.STATE_ZERO.equals(dateSlot)){//二月年化收益率折线图
			dateInterval=CacheConsts.TWOMONTH;
		}
		
		Map<String, Object> queryList = this.commonQueryList(dateSlot, dateInterval);
		if(RetCode.FAIL.equals(queryList.get(StringUtil.trim(CommonConsts.RETCODE)))){
			logger.info("【调用接口查询"+dateInterval+"列表是失败】");
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433044);
		}
		List<Map<String,String>> list =(List<Map<String,String>>) queryList.get(CommonConsts.FUNDRATELIST);
		logger.info("【调用接口查询获取"+dateInterval+"列表】list="+list);
		
		if(StringUtil.isNotEmpty(StringUtil.trim(list))){
			int size = list.size();
			logger.info("【获取列表数】size="+size);
			if(size!=0){
				BigDecimal annual7DSum = new BigDecimal(0);  //总年化率 
				BigDecimal unitAccruaSum = new BigDecimal(0);   //总万分收益 
				BigDecimal maxDwjz = new BigDecimal(0);  //最大万分收益 
				BigDecimal maxSyl = new BigDecimal(0);  //最大年化率 
				Map<String,String> mapOne = list.get(0);
				BigDecimal minDwjz =  new BigDecimal(mapOne.get(CommonConsts.UNITACCRUA));  //最小万分收益 
				BigDecimal minSyl =  new BigDecimal(mapOne.get(CommonConsts.ANNUAL7D));  //最小年化率 
				for (Map<String, String> map : list) {
					//对应时间(yyyy-MM-dd)
					map.put(CommonConsts.OPPOSITEDATE,DateTimesUtil.date10(StringUtil.trim(map.get(CommonConsts.OPPOSITEDATE))));
					BigDecimal annual7D = new BigDecimal(map.get(CommonConsts.ANNUAL7D));
					BigDecimal unitAccrua = new BigDecimal(map.get(CommonConsts.UNITACCRUA));
					annual7DSum = annual7DSum.add(annual7D);
					unitAccruaSum = unitAccruaSum.add(unitAccrua);
					maxDwjz =maxDwjz.max(unitAccrua);
					minDwjz =minDwjz.min(unitAccrua);
					maxSyl = maxSyl.max(annual7D);
					minSyl = minSyl.min(annual7D);
					map.put(CommonConsts.ANNUAL7D, StringUtil.trim(FormatUtil.setScale4(map.get(CommonConsts.ANNUAL7D))));//年化率
					//万分收益
					map.put(CommonConsts.UNITACCRUA, StringUtil.trim(FormatUtil.setScale4(map.get(CommonConsts.UNITACCRUA))));
				}
				
				BigDecimal scale = new BigDecimal(size); //列表数
				String aveSyl =StringUtil.trim(annual7DSum.divide(scale,4,BigDecimal.ROUND_HALF_UP)) ;//平均年化率 
				String aveDwjz = StringUtil.trim(unitAccruaSum.divide(scale,4,BigDecimal.ROUND_HALF_UP));//平均万分收益
				Map<String, String> mapLast = list.get(size - 1);
				result.put(CommonConsts.SYL,StringUtil.trim(FormatUtil.setScale4(StringUtil.trim(mapLast.get(CommonConsts.ANNUAL7D)))));
				result.put(CommonConsts.AVESYL,aveSyl);
				result.put(CommonConsts.AVEDWJZ,aveDwjz);
				result.put(CommonConsts.MAXSYL,StringUtil.trim(FormatUtil.setScale4(maxSyl)));
				result.put(CommonConsts.MINSYL,StringUtil.trim(FormatUtil.setScale4(minSyl)));
				result.put(CommonConsts.MAXDWJZ,StringUtil.trim(FormatUtil.setScale4(maxDwjz)));
				result.put(CommonConsts.MINDWJZ,StringUtil.trim(FormatUtil.setScale4(minDwjz)));
				result.put(CommonConsts.STARTDATE,StringUtil.trim(mapOne.get(CommonConsts.OPPOSITEDATE)));
				result.put(CommonConsts.ENDDATE,StringUtil.trim(mapLast.get(CommonConsts.OPPOSITEDATE)));
				
			}
		}
		
		result.put(CommonConsts.FUNDRATELIST,list);
		logger.info("年化率、万份收益图表库返回页面数据result="+result);
		return ResultJsonUtil.resultJson(RetCode.SUCCESS,CommonConsts.SPACE,result);
		
	}
	
	@SuppressWarnings("unchecked")
	public Map<String,Object> commonQueryList(String dateSlot,String dateInterval){
		Map<String, Object> ress = new HashMap<String, Object>();
		if (StringUtil.isEmpty(serviceuUrl_fpbg)) {
			logger.info("【获取七日年化率路径ip失败】");
			ress.put(CommonConsts.RETCODE,RetCode.FAIL);
			return ress;
		}
		String fpbg_S01003 = UmfPropModel.INSTANCE.getPropValue(ConfigConsts.FPBG_S01003);
		if (StringUtil.isEmpty(fpbg_S01003)) {
			logger.info("【获取七日年化率路径方法失败】");
			ress.put(CommonConsts.RETCODE,RetCode.FAIL);
			return ress;
		}
		
		
		//获取当前时间
		Calendar calendar = Calendar.getInstance();
		String endDate = DateTimesUtil.date8(calendar.getTime());
		if(ValueConsts.STATE_ONE.equals(dateSlot)){
			calendar.add(Calendar.DATE, -6);//前一周
		}else if(ValueConsts.STATE_TWO.equals(dateSlot)){
			calendar.add(Calendar.DATE, -29);//前一月
		}else if(ValueConsts.STATE_THREE.equals(dateSlot)){
			calendar.add(Calendar.DATE, -89);//前三月
		}else if(ValueConsts.ZERO.equals(dateSlot)){
			calendar.add(Calendar.DATE, -59);//前两月
		}
		
		// 获取截止时间
		String startDate = DateTimesUtil.date8(calendar.getTime());
		
		//startDate="20150425";
		//startDate="20150401";
		//startDate="20150518";
		//endDate="20150422";TODO
		//fundChannelCode="32012000";
		//fundNo="000686";
		Map<String, Object> params = new HashMap<String, Object>();
		// 输入参数
		com.umfwechat.common.ReqMessage makeFundReq = ReqMessageUtil.makeFundReq();
		params.put(CommonConsts.RPID,makeFundReq.getRpid());
		params.put(CommonConsts.REQDATE, makeFundReq.getReqDate());//日期
		params.put(CommonConsts.REQTIME, makeFundReq.getReqTime());//时间
		params.put(CommonConsts.VERSION, ValueConsts.VERSION);//版本号
		params.put(CommonConsts.FUNDCHANNELCODE, fundChannelCode);
		params.put(CommonConsts.FUNDNO, fundNo);
		params.put(CommonConsts.STARTDATE, startDate);
		params.put(CommonConsts.ENDDATE, endDate);
		Map<String, Object> res = null;
		List<Map<String,Object>> list=null;
		List<Map<String,String>> fundRateList=new ArrayList<Map<String,String>>();
		try {
			logger.info("【年化收益率折线图查询,"+dateInterval+"数据】请求数据：params" + params+ "访问路径url=" + serviceuUrl_fpbg+fpbg_S01003);
			res = HttpUtil.httpPostForm2Xml(serviceuUrl_fpbg+fpbg_S01003, params);
			logger.info("【年化收益率折线图查询,"+dateInterval+"数据】返回结果：="+res);
			if (RetCode.SUCCESS.equals(StringUtil.trim(res.get(CommonConsts.RETCODE)))) { // 查询成功
				
				list = JSONArray.toList(JSONArray.fromObject(res.get(CommonConsts.FUNDRATELIST)), Map.class);
				if(StringUtil.isNotEmpty(StringUtil.trim(list))){
					int size = list.size();
					logger.info("【年化收益率折线图查询成功】size="+size);
					if (size != 0) {
						for(Map<String,Object> fundRateMap : list){
							Map<String,String> dataMap = new HashMap<String,String>();
							//对应时间(yyyy-MM-dd)
							dataMap.put(CommonConsts.OPPOSITEDATE,StringUtil.trim(fundRateMap.get(CommonConsts.OPPOSITEDATE)));
							//七日年化（货币基金才有）
							dataMap.put(CommonConsts.ANNUAL7D,StringUtil.trim(fundRateMap.get(CommonConsts.ANNUAL7D)));
							//万分收益
							dataMap.put(CommonConsts.UNITACCRUA,StringUtil.trim(fundRateMap.get(CommonConsts.UNITACCRUA)));
							fundRateList.add(dataMap);
						}
						logger.info("【年化收益率折线图查询成功】将获取的列表list="+fundRateList);
						fundRateList = SortUtil.sortListAsc(fundRateList, CommonConsts.OPPOSITEDATE);
						//RedisUtil.setObj(CacheConsts.SAASWX+dateInterval+CacheConsts.LINECHARTLIST+endDate, PropertyConsts.ONEDAY, fundRateList);
						//RedisUtil.setObj(CacheConsts.SAASWX+dateInterval+CacheConsts.LINECHARTLIST+CacheConsts.YESTERDAY, PropertyConsts.ONEWEEK, fundRateList);
						logger.info("【年化收益率折线图查询成功】将获取的列表排序后list="+fundRateList);
						ress.put(CommonConsts.RETCODE,RetCode.SUCCESS);
						ress.put(CommonConsts.FUNDRATELIST,fundRateList);
						return ress;
					}else{
						logger.info("【年化收益率折线图查询成功】获取数据列表size="+size);
					}
				}
			} 
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("【年化收益率折线图查询异常】"+e);
		}
		//fundRateList = (List<Map<String,String>>)RedisUtil.getObj(CacheConsts.SAASWX+dateInterval+CacheConsts.LINECHARTLIST+CacheConsts.YESTERDAY);
		//logger.info(String.format("【从redis中获取前一天的数据】list="+fundRateList));
		ress.put(CommonConsts.RETCODE,RetCode.SUCCESS);
		ress.put(CommonConsts.FUNDRATELIST,fundRateList);
		return ress;
	}
	
	/**
	 * 计算器初始化页面
	 * @param dateSlot
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/calculatorQuery",produces="application/json;charset=UTF-8")
	@ResponseBody
	public String calculatorQuery(String dateSlot){	
		
		Map<String, Object> result = new HashMap<String, Object>();
		
		if (StringUtil.isEmpty(fundNo)) {
			logger.info("【获取基金编码失败】");
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433044);
		}
		if (StringUtil.isEmpty(fundChannelCode)) {
			logger.info("【获取基金渠道编码失败】");
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433044);
		}
		
		String defaultMoney = UmfPropModel.INSTANCE.getPropValue(ConfigConsts.DEFAULTMONEY);
		if (StringUtil.isEmpty(defaultMoney)) {
			logger.info("【获取默认金额失败】");
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433044);
		}
		
		String dateInterval=CacheConsts.TWOMONTH;//两月
		dateSlot=ValueConsts.ZERO;
		Map<String, Object> queryList = this.commonQueryList(dateSlot, dateInterval);
		if(RetCode.FAIL.equals(queryList.get(StringUtil.trim(CommonConsts.RETCODE)))){
			logger.info("【获取近两个月的万分收益及年化率列表查询失败】");
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433044);
		}
		List<Map<String,String>> list =(List<Map<String,String>>) queryList.get(CommonConsts.FUNDRATELIST);
		logger.info("【调接口获取近两个月的万分收益及年化率列表】list="+list);
		
		if(StringUtil.isNotEmpty(StringUtil.trim(list))){
			int size = list.size();
			logger.info("【从近两个月的万分收益及年化率列表数】size="+size);
			if(size!=0){
				BigDecimal annual7DSum = new BigDecimal(0);  //总年化率 
				BigDecimal unitAccruaSum = new BigDecimal(0);   //总万分收益 
				for (Map<String, String> map : list) {
					BigDecimal annual7D = new BigDecimal(map.get(CommonConsts.ANNUAL7D));//年化率 
					BigDecimal unitAccrua = new BigDecimal(map.get(CommonConsts.UNITACCRUA));//万分收益
					annual7DSum = annual7DSum.add(annual7D);
					unitAccruaSum = unitAccruaSum.add(unitAccrua);
				}
				
				BigDecimal scale = new BigDecimal(size); //列表数量
				String aveSyl = StringUtil.trim(annual7DSum.divide(scale,2,BigDecimal.ROUND_HALF_UP));//平均年化率 
				String aveDwjz =StringUtil.trim(unitAccruaSum.divide(scale,4,BigDecimal.ROUND_HALF_UP));//平均万分收益
				result.put(CommonConsts.AVESYL,aveSyl);
				result.put(CommonConsts.AVEDWJZ,aveDwjz);
			}
		}
		
		//获取当前时间
		Calendar calendar = Calendar.getInstance();
		String endDate= DateTimesUtil.date10(calendar.getTime());
		calendar.add(Calendar.DATE, -59);
		// 获取截止时间
		String startDate = DateTimesUtil.date10(calendar.getTime());
		result.put(CommonConsts.STARTDATE,startDate);//前两个月日期
		result.put(CommonConsts.ENDDATE,endDate);//今日日期
		result.put(CommonConsts.DEFAULTMONEY,defaultMoney);//默认金额
		logger.info("计算器初始化页面返回页面result="+result);
		return ResultJsonUtil.resultJson(RetCode.SUCCESS,CommonConsts.SPACE,result);
	}
	
	
	
}
